
package com.insigma.business.qrcode.soldier._provider.impl;


import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.CommonAuthInfoDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.*;
import com.insigma.business.qrcode.soldier.entity.CommonAuthInfoDO;
import com.insigma.business.qrcode.soldier._provider.CommonAuthInfoProvider;
import com.insigma.business.qrcode.soldier.service.CommonAuthInfoService;
import com.insigma.business.qrcode.common.converter.CommonAuthInfoProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonAuthInfoProvider")
public class CommonAuthInfoProviderImpl extends BaseProviderImpl implements CommonAuthInfoProvider {

    @Autowired
    private CommonAuthInfoService commonAuthInfoService;


    @Override
    public BasePagingResponse<List<CommonAuthInfoDTO>> searchCommonAuthInfo(final CommonAuthInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonAuthInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonAuthInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonAuthInfoBSearcher commonAuthInfoBSearcher = CommonAuthInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonAuthInfoDO> commonAuthInfoDOList = commonAuthInfoService.search(commonAuthInfoBSearcher);
                final List<CommonAuthInfoDTO> commonAuthInfoDTOList = CommonAuthInfoProviderConverter.convertResponseDOList2DTOList(commonAuthInfoDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonAuthInfoDTOList, searchRequest,commonAuthInfoBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_AUTH_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonAuthInfoDTO> getCommonAuthInfo(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonAuthInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonAuthInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonAuthInfoDO commonAuthInfoDO = commonAuthInfoService.get((String)baseGetRequest.getId());
                final CommonAuthInfoDTO remoteObj = CommonAuthInfoProviderConverter.convertResponseDO2DTO(commonAuthInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_AUTH_INFO_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonAuthInfo(final CommonAuthInfoInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonAuthInfo的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CommonAuthInfoDO commonAuthInfoDO = CommonAuthInfoProviderConverter.convertRequestIO2DO(insertRequest);
                commonAuthInfoService.insert(commonAuthInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonAuthInfoDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_AUTH_INFO_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonAuthInfo(final CommonAuthInfoUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonAuthInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CommonAuthInfoDO commonAuthInfoDO = CommonAuthInfoProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = commonAuthInfoService.update(commonAuthInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_AUTH_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCommonAuthInfo(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonAuthInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = commonAuthInfoService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_AUTH_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public String getAdminAreaCode(String auth) {

        BizCheckUtil.checkNotBlank(auth, "auth不能为空");
        CommonAuthInfoDO byAuth = commonAuthInfoService.getByAuth(auth);
        BizCheckUtil.checkNotNull(byAuth, "暂无权限！");
        return byAuth.getAdminAreaCode();
    }
}
